﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Site.Secure
{
    public partial class Add_Worker : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool Invalid = false;
            try
            {
                if (Session["userType"].ToString() == "admin" || Session["userType"].ToString() == "root") //If the current user is a admin allow them to continue
                {
                    if (edtID.Text.Length != 13)
                    {
                        valID.Visible = true;
                        Invalid = true;
                    }
                    if (edtName.Text.Length < 1)
                    {
                        valName.Visible = true;
                        Invalid = true;
                    }
                    if (edtSurname.Text.Length < 1)
                    {
                        valSurname.Visible = true;
                        Invalid = true;
                    }
                    if (edtAddress.Text.Length < 1)
                    {
                        valAddress.Visible = true;
                        Invalid = true;
                    }
                    if (edtCell.Text.Length < 10)
                    {
                        valCell.Visible = true;
                        Invalid = true;
                    }
                    if (edtPID.Text.Length < 1)
                    {
                        valPhone.Visible = true;
                        Invalid = true;
                    }


                    if (Invalid)
                        return;

                    string WID = edtName.Text.Substring(0,4) +  edtSurname.Text.Substring(0,4) + edtID.Text.Substring(0, 2);
                    WID = WID.ToUpper();
                    SqlDataSource1.InsertCommand = "INSERT INTO Workers (Name, Surname, Email, Contact_Cellphone, Contact_Home, Address, WID, Worker_Type, ID_Number, Drivers_License, PID, Notes) VALUES (@Name, @Surname, @Email, @Contact_Cellphone, @Contact_Home, @Address, @WID, @Worker_Type, @ID_Number, @Drivers_License, @PID, @Notes)";
                    SqlDataSource1.InsertParameters.Add("Name", edtName.Text);
                    SqlDataSource1.InsertParameters.Add("Surname", edtSurname.Text);
                    SqlDataSource1.InsertParameters.Add("Email", edtEmail.Text);
                    SqlDataSource1.InsertParameters.Add("Contact_Cellphone", edtCell.Text);
                    SqlDataSource1.InsertParameters.Add("Contact_Home", edtHome.Text);
                    SqlDataSource1.InsertParameters.Add("Address", edtAddress.Text);
                    SqlDataSource1.InsertParameters.Add("WID", WID);
                    SqlDataSource1.InsertParameters.Add("Worker_Type", edtType.Text);
                    SqlDataSource1.InsertParameters.Add("ID_Number", edtID.Text);
                    SqlDataSource1.InsertParameters.Add("Drivers_License", TypeCode.Boolean, edtDrivers.Text);
                    SqlDataSource1.InsertParameters.Add("PID", edtPID.Text);
                    SqlDataSource1.InsertParameters.Add("Notes", edtNotes.Text);
                    SqlDataSource1.Insert();
                    Response.Redirect("Default.aspx?OperationStatus=" + "Adding Worker Complete, Worker ID is'" + WID + "'");
                }
                else
                {
                    System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('You are not allowed to add users, please login with an account that has full root access')</SCRIPT>");
                }
            }
            catch (NullReferenceException ex)
            {
                //If you get here it means that Session == NULL, requires user to re-login
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert(Your session has expired, this means that you can not perform any root tasks on this website. Please login again or restart your browser and navigate back to this page.')</SCRIPT>");
            }
            
        }
    }
}